$().ready(function() {
	set_fileupload();
	adddiary_set_fileupload();
	get_hash();
	$("#submitDiary").click(function(){
		saveDiary();
	});
	$("#add_diary_form").click(function(){
		addDiaryForm();
	});
	$("#addsubmitdiary").click(function(){
		addDiary();
	});
});
function list_diary(){
	var control;
	var asort = 0;
	html_head = {};
	diary_icons = {};
	xfunc = "diary/list_diary";
	
	data_sent = {};
		
		$.ajax({ 
			type: "POST", // request type POST
			url: base_url+xfunc, // post to PHP file
			data: data_sent, // request send to PHP file
			dataType:'json',
			success: function(result){ // result = return from php request
				$(result.intID).each(function(k,v){
					asort = result.intID[k];
					diary_icons_panel = '';
						diary_icons_panel +='<tr>';
					    	diary_icons_panel +='<td><img width="120" class="img-rounded" src="'+base_url+'assets/uploaded/img/thumbnail/'+result.strImgLink[k]+'" alt=""/></td>';
							diary_icons_panel +='<td><span class="username">'+result.strUserName[k]+'</span></td>';
							diary_icons_panel +='<td>'+result.strTitle[k]+'</td>';
							diary_icons_panel +='<td width="90">'+result.dateCreatedDate[k]+'</td>';
							diary_icons_panel +='<td class="controls"><div class="btn-group">';
							diary_icons_panel +='<button class="btn btn-primary" onclick="javascript:editDiary(\''+result.intID[k]+'\')"><i class="icon-edit"></i>Edit</button>';
							diary_icons_panel +='<button class="btn btn-danger" onclick="javascript:delDiary(\''+result.intID[k]+'\', \''+result.strTitle[k]+'\')"><i class="icon-trash"></i>Delete</button>';
							diary_icons_panel +='</td>';
							diary_icons_panel +='</tr>';
						diary_icons[result.intID[k]]=diary_icons_panel;
				});
				
				diary_html_panel = '<table class="table table-striped table-bordered" id="diary_table">';
				diary_html_panel += '<thead>';
					diary_html_panel += '<tr>';
						diary_html_panel += '<th class="thumb-photo">Thumbnail</th>';
						diary_html_panel += '<th class="note-title">Username</th>';
						diary_html_panel += '<th class="note-title">Title</th>';
						diary_html_panel += '<th class="note-title">Date Note</th>';
						diary_html_panel += '<th>Control</th>';
					diary_html_panel += '</tr>';
				diary_html_panel += '</thead>';
				diary_html_panel += '<tbody>';
				for(x=0;x<=asort;x++){
					if($.trim(diary_icons[x])!=""){
						diary_html_panel +=diary_icons[x];
					}
								
				}		
				diary_html_panel += '</tbody>';		
				diary_html_panel += '</table>';
				
				$('#diary_content').html(diary_html_panel);
				$('#diary_table').dataTable({
					"sPaginationType": "full_numbers",
					"bLengthChange": false,
					"bSort": false
				});
			}
		});	
}
function addDiaryForm(){
	$("#diary_content, #add_diary_form").hide();
	$("#addDiary").slideDown('slow', function(){
		$("#diary_content").hide();
		$("#addDiary").slideDown('slow', function(){
			$(".back-btn-adddiary a").attr("onclick","btn_back_adddiary()");
		});	
	});	
}

function addDiary(){
	var strNoteTitle = $("#adddiarystrNoteTitle").val();
	var strNotes = $("#adddiarystrNotes").val();
	var strImgLink = $("#addDiarystrImgLink").val();
	if(strNoteTitle==''){
		$("#errorDiary").html("<div class='alert alert-error'>Error: Put the title!</div>");
	} else if(strNotes=='') {
		$("#errorDiary").html("<div class='alert alert-error'>Error: Put the Notes!</div>");
	} else if(strImgLink=='') {
		$("#errorDiary").html("<div class='alert alert-error'>Error: Put the image thumbnail!</div>");
	} else {
		xfunc = "diary/save_diary";
		data_sent = {
				'strTitle':strNoteTitle,
				'strNotes':strNotes,
				'strImgLink':strImgLink
		};
		$.ajax({
			type: 'POST',
			url: base_url+xfunc,
			data: data_sent, 
			dataType:'json',
			success: function(result){
				$("#errorDiary").hide();
				$("#adddiarySuccess").html('<div class="alert alert-success">Data was successfully saved!</div>');
				
				$("#adddiarystrNoteTitle").val('');
				$("#adddiarystrNotes").val('');
				$("#addDiarystrImgLink").val('');
				$(".alert-success").fadeOut(5000);
			}
		});
	}
}
function editDiary(noteID){
	xfunc = "diary/get_diary";
	data_sent = {
			'intID':noteID
	};
	$.ajax({
		type: 'POST',
		url: base_url+xfunc,
		data: data_sent, 
		dataType:'json',
		success: function(result){
			var diary_thumbnail = '<div class="imgThumbnail"><img width="86" class="img-rounded" src="'+base_url+'assets/uploaded/img/thumbnail/'+result.strImgLink+'" alt=""/></div>';
			$("#intDiaryID").val(result.intID);
			$("#strDiaryTitle").val(result.strTitle);
			$("#strDiaryNotes").val(result.strNotes);
			$("#diary_thumbnail").html(diary_thumbnail);
			$("#diary_thumbnailLink").val(result.strImgLink);
		}
	});

	$("#add_diary_form").hide();
	$("#diary_content").hide();
	$("#editDiary").slideDown('slow', function(){
		$(".back-btn-diary a").attr("onclick","btn_back_diary()");
	});	
}
function saveDiary(){
	var intID = $("#intDiaryID").val();
	var strTitle = $("#strDiaryTitle").val();
	var strNotes = $("#strDiaryNotes").val();
	var strImgLink = $("#diary_thumbnailLink").val();
	
	xfunc = "diary/save_diary";
	data_sent = {
			'intID':intID,
			'strTitle':strTitle,
			'strNotes':strNotes,
			'strImgLink':strImgLink
	};
	
	$.ajax({
		type: 'POST',
		url: base_url+xfunc,
		data: data_sent, 
		dataType:'json',
		success: function(result){
			$("#diarySuccess").html('<div class="alert alert-success">Data was successfully updated!</div>');
			$(".alert-success").fadeOut(5000);
		}
	});
}
function delDiary(noteID, titleName){
	xfunc = "diary/delete_diary";
	data_sent = {
			'intID':noteID
	};
	$('#label-delete').html(titleName);
    $('#deleteModal').modal('show');
    $('button#confirm').click(function(e){
        $('#deleteModal').modal('hide');
		
		$.ajax({
			type: 'POST',
			url: base_url+xfunc,
			data: data_sent, 
			dataType:'json',
			success: function(result){
				list_diary();
			}
		});
    });
}
function btn_back_adddiary(){
	$("#addDiary").hide();
	$("#diary_content").slideDown('fast', function(){
		$("#add_diary_form").show();
		list_diary();
	});	
}
function btn_back_diary(){
	$("#editDiary").hide();
	$("#diary_content").slideDown('fast', function(){
		$("#add_diary_form").show();
		list_diary();
	});	
}
function set_fileupload(){
	$('#input_fileupload').attr('data-url',base_url+'assets/lib/jquery.fileupload/server/php/index.php');
	file_upload();
}
function file_upload(){
	$('.fileupload').fileupload();
	$('.fileupload').fileupload('option',{
		dataType: 'json',
		maxFileSize: 20000000, //20MB
		acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,			
		done: function (e, data) {
			$.each(data.result, function (index, file) {
				$.each(file, function (k, v) {		
					html = '<img width="86" class="img-rounded" src="'+base_url+"assets/uploaded/img/thumbnail/"+v.name+'" />';
					$("#diary_thumbnailLink").val(v.name);			
					$(".imgThumbnail").hide();
					$(".imgThumbnail").slideDown('slow', function(){
						$(".imgThumbnail").html(html);
					});					
				});
			});
		}
	}).bind('fileuploadfailed', function (e, data) {
		$(".result-message-box").html("Upload Error!");
	});		
}

//ADD DATA
function adddiary_set_fileupload(){
	$('#adddiary_input_fileupload').attr('data-url',base_url+'assets/lib/jquery.fileupload/server/php/index.php');
	adddiary_file_upload();
}
function adddiary_file_upload(){
	$('.adddiary_fileupload').fileupload();
	$('.adddiary_fileupload').fileupload('option',{
		dataType: 'json',
		maxFileSize: 20000000, //20MB
		acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,			
		done: function (e, data) {
			$.each(data.result, function (index, file) {
				$.each(file, function (k, v) {		
					html = '<img width="86" class="img-rounded" src="'+base_url+"assets/uploaded/img/thumbnail/"+v.name+'" />';
					$("#addDiarystrImgLink").val(v.name);			
					$("#adddiary_thumbnail").html(html);			
				});
			});
		}
	}).bind('fileuploadfailed', function (e, data) {
		$(".result-message-box").html("Upload Error!");
	});		
}